package com.example.wordbook;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class WordItemAdapter_List extends ArrayAdapter<WordItem> {
    private int resourceId;
    private List<WordItem> wordItemList = new ArrayList<WordItem>();

    public WordItemAdapter_List(Context context, int textViewResourceId, List<WordItem> objects) {
        super(context, textViewResourceId, objects);
        this.resourceId = textViewResourceId;
        this.wordItemList = (List<WordItem>) objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        WordItem wordItem = getItem(position);
        View view;
        ViewHolder viewHolder;
        if(convertView == null) {
            view = view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.word = (TextView)view.findViewById(R.id.word);
            viewHolder.meaning = (TextView)view.findViewById(R.id.meaning);
            viewHolder.button = (Button)view.findViewById(R.id.delete_one);
            view.setTag(viewHolder);
        }
        else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.word.setText(wordItem.getWord());
        viewHolder.meaning.setText(wordItem.getMeaning());
        viewHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                final View input_layout = LayoutInflater.from(getContext()).inflate(R.layout.delete_pop, null);
                TextView deleteWordTV = (TextView) input_layout.findViewById(R.id.delete_word);
                TextView deleteMeaningTV = (TextView) input_layout.findViewById(R.id.delete_meaning);
                deleteWordTV.setText(wordItem.getWord());
                deleteMeaningTV.setText(wordItem.getMeaning());
                builder.setTitle("删除词汇").setIcon(android.R.drawable.ic_dialog_info).setView(input_layout)
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        LitePal.deleteAll(WordItem.class, "word = ?", wordItem.getWord());
                        wordItemList.remove(position);
                        notifyDataSetChanged();
                    }
                });
                builder.show();
            }
        });
        return view;
    }

    class ViewHolder{
        TextView word;
        TextView meaning;
        Button button;
    }
}
